using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
// using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using ImageUpload;
using System.Collections.Generic;

using System.Data;
using System.Data.Linq;
using System.Linq;
using EnCryptDecrypt;

namespace ImageUpload {
    public class FTPSettings : System.Windows.Forms.Form {
        private System.Windows.Forms.Button btnSave;
        private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.Label label8;
        private System.Windows.Forms.Label label9;
        private System.Windows.Forms.Label label10;
        private System.Windows.Forms.TextBox tbName;
        private System.Windows.Forms.TextBox tbPictureFolder;
        private System.Windows.Forms.TextBox tbPass;
        private System.Windows.Forms.TextBox tbUser;
        private System.Windows.Forms.TextBox tbFtp;
        private System.Windows.Forms.TextBox tbThumbFolder;
        private System.Windows.Forms.TextBox tbThumbPrefix;
        private System.Windows.Forms.Label label11;

        private const string ACCOUNTSFILENAME = "accounts.xml";

        private Guid accountVID;
        private Account account;
        private System.Windows.Forms.TextBox tbPicturePrefix;
        private System.Windows.Forms.Label label12;
        private ImageUpload.ResizeSettingsControl rsc;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public FTPSettings() {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            //
            // TODO: Add any constructor code after InitializeComponent call
            //

            InitAccount(account);
        }

        private void SetDefaults() {
            rsc.JpegQuality = ThumbMaker.DEFAULTJPEGQUALITY;
            rsc.ThumbHeight = 120;
            rsc.ThumbWidth = 160;
            rsc.BigHeight = 600;
            rsc.BigWidth = 800;
        }

        private void InitAccount(Account ftpAccount) {
            if (ftpAccount != null) {
                if (ftpAccount.JpegQuality == 0) ftpAccount.JpegQuality = ThumbMaker.DEFAULTJPEGQUALITY;
                tbName.Text = ftpAccount.Name;
                tbFtp.Text = ftpAccount.Url;
                tbUser.Text = ftpAccount.Username;
                tbPass.Text = CryptorEngine.Decrypt(ftpAccount.Password, true, ftpAccount.Username);
                tbPictureFolder.Text = ftpAccount.ImageFolder;
                tbPicturePrefix.Text = ftpAccount.ImagePrefix;
                tbThumbFolder.Text = ftpAccount.ThumbFolder;
                tbThumbPrefix.Text = ftpAccount.ThumbPrefix;
                rsc.BigWidth = ftpAccount.ImageWidth;
                rsc.BigHeight = ftpAccount.ImageHeight;
                rsc.ThumbHeight = ftpAccount.ThumbHeight;
                rsc.ThumbWidth = ftpAccount.ThumbWidth;
                rsc.JpegQuality = ftpAccount.JpegQuality;
            }
            else {
                SetDefaults();
            }
        }

        public FTPSettings(Guid vid) {
            InitializeComponent();
            List<Account> accounts = GetAccounts();
            foreach (Account xmlAccount in accounts) {
                if (xmlAccount.Id == vid) {
                    account = xmlAccount;
                    accountVID = vid;
                }
            }
            InitAccount(account);
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing) {
            if (disposing) {
                if (components != null) {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FTPSettings));
            this.btnSave = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.label8 = new System.Windows.Forms.Label();
            this.label9 = new System.Windows.Forms.Label();
            this.tbName = new System.Windows.Forms.TextBox();
            this.tbPictureFolder = new System.Windows.Forms.TextBox();
            this.tbPass = new System.Windows.Forms.TextBox();
            this.tbUser = new System.Windows.Forms.TextBox();
            this.tbFtp = new System.Windows.Forms.TextBox();
            this.tbThumbFolder = new System.Windows.Forms.TextBox();
            this.label10 = new System.Windows.Forms.Label();
            this.tbThumbPrefix = new System.Windows.Forms.TextBox();
            this.label11 = new System.Windows.Forms.Label();
            this.tbPicturePrefix = new System.Windows.Forms.TextBox();
            this.label12 = new System.Windows.Forms.Label();
            this.rsc = new ResizeSettingsControl();
            this.SuspendLayout();
            // 
            // btnSave
            // 
            // this.btnSave.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.btnSave.Location = new System.Drawing.Point(208, 8);
            this.btnSave.Name = "btnSave";
            this.btnSave.TabIndex = 17;
            this.btnSave.Text = "Save";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.Location = new System.Drawing.Point(208, 40);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.TabIndex = 18;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(152, 8);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(40, 23);
            this.label5.TabIndex = 99;
            this.label5.Text = "Name";
            // 
            // label6
            // 
            this.label6.Location = new System.Drawing.Point(152, 80);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(40, 23);
            this.label6.TabIndex = 99;
            this.label6.Text = "pass";
            // 
            // label7
            // 
            this.label7.Location = new System.Drawing.Point(152, 56);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(40, 23);
            this.label7.TabIndex = 99;
            this.label7.Text = "user";
            // 
            // label8
            // 
            this.label8.Location = new System.Drawing.Point(152, 32);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(40, 23);
            this.label8.TabIndex = 99;
            this.label8.Text = "ftp://";
            // 
            // label9
            // 
            this.label9.Location = new System.Drawing.Point(152, 104);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(112, 23);
            this.label9.TabIndex = 99;
            this.label9.Text = "big picture folder";
            // 
            // tbName
            // 
            this.tbName.Location = new System.Drawing.Point(8, 8);
            this.tbName.Name = "tbName";
            this.tbName.Size = new System.Drawing.Size(136, 20);
            this.tbName.TabIndex = 1;
            this.tbName.Text = "";
            // 
            // tbPictureFolder
            // 
            this.tbPictureFolder.Location = new System.Drawing.Point(8, 104);
            this.tbPictureFolder.Name = "tbPictureFolder";
            this.tbPictureFolder.Size = new System.Drawing.Size(136, 20);
            this.tbPictureFolder.TabIndex = 5;
            this.tbPictureFolder.Text = "";
            // 
            // tbPass
            // 
            this.tbPass.Location = new System.Drawing.Point(8, 80);
            this.tbPass.Name = "tbPass";
            this.tbPass.PasswordChar = '*';
            this.tbPass.Size = new System.Drawing.Size(136, 20);
            this.tbPass.TabIndex = 4;
            this.tbPass.Text = "";
            // 
            // tbUser
            // 
            this.tbUser.Location = new System.Drawing.Point(8, 56);
            this.tbUser.Name = "tbUser";
            this.tbUser.Size = new System.Drawing.Size(136, 20);
            this.tbUser.TabIndex = 3;
            this.tbUser.Text = "";
            // 
            // tbFtp
            // 
            this.tbFtp.Location = new System.Drawing.Point(8, 32);
            this.tbFtp.Name = "tbFtp";
            this.tbFtp.Size = new System.Drawing.Size(136, 20);
            this.tbFtp.TabIndex = 2;
            this.tbFtp.Text = "";
            // 
            // tbThumbFolder
            // 
            this.tbThumbFolder.Location = new System.Drawing.Point(8, 152);
            this.tbThumbFolder.Name = "tbThumbFolder";
            this.tbThumbFolder.Size = new System.Drawing.Size(136, 20);
            this.tbThumbFolder.TabIndex = 7;
            this.tbThumbFolder.Text = "";
            // 
            // label10
            // 
            this.label10.Location = new System.Drawing.Point(152, 152);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(112, 23);
            this.label10.TabIndex = 99;
            this.label10.Text = "thumb picture folder";
            // 
            // tbThumbPrefix
            // 
            this.tbThumbPrefix.Location = new System.Drawing.Point(8, 176);
            this.tbThumbPrefix.Name = "tbThumbPrefix";
            this.tbThumbPrefix.Size = new System.Drawing.Size(136, 20);
            this.tbThumbPrefix.TabIndex = 8;
            this.tbThumbPrefix.Text = "";
            // 
            // label11
            // 
            this.label11.Location = new System.Drawing.Point(152, 176);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(112, 23);
            this.label11.TabIndex = 99;
            this.label11.Text = "thumb picture prefix";
            // 
            // tbPicturePrefix
            // 
            this.tbPicturePrefix.Location = new System.Drawing.Point(8, 128);
            this.tbPicturePrefix.Name = "tbPicturePrefix";
            this.tbPicturePrefix.Size = new System.Drawing.Size(136, 20);
            this.tbPicturePrefix.TabIndex = 6;
            this.tbPicturePrefix.Text = "";
            // 
            // label12
            // 
            this.label12.Location = new System.Drawing.Point(152, 128);
            this.label12.Name = "label12";
            this.label12.Size = new System.Drawing.Size(112, 23);
            this.label12.TabIndex = 99;
            this.label12.Text = "big picture prefix";
            // 
            // rsc
            // 
            this.rsc.BigHeight = 768;
            this.rsc.BigWidth = 1024;
            this.rsc.JpegQuality = 75;
            this.rsc.Location = new System.Drawing.Point(0, 200);
            this.rsc.Name = "rsc";
            this.rsc.Size = new System.Drawing.Size(296, 136);
            this.rsc.TabIndex = 100;
            this.rsc.ThumbHeight = 120;
            this.rsc.ThumbWidth = 160;
            // 
            // FTPSettings
            // 
            this.AcceptButton = this.btnSave;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.btnCancel;
            this.ClientSize = new System.Drawing.Size(296, 334);
            this.Controls.Add(this.rsc);
            this.Controls.Add(this.tbPicturePrefix);
            this.Controls.Add(this.tbThumbPrefix);
            this.Controls.Add(this.tbThumbFolder);
            this.Controls.Add(this.tbFtp);
            this.Controls.Add(this.tbUser);
            this.Controls.Add(this.tbPass);
            this.Controls.Add(this.tbPictureFolder);
            this.Controls.Add(this.tbName);
            this.Controls.Add(this.label12);
            this.Controls.Add(this.label11);
            this.Controls.Add(this.label10);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.label8);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnSave);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "FTPSettings";
            this.Text = "FTP Settings";
            this.Load += new System.EventHandler(this.FTPSettings_Load);
            this.ResumeLayout(false);

        }
        #endregion

        private void btnCancel_Click(object sender, System.EventArgs e) {
            this.Close();
        }

        private void btnSave_Click(object sender, System.EventArgs e) {
            if (Valid()) {
                if (account == null) {
                    account = new Account();
                    account.Id = Guid.NewGuid();
                    FormGM.DataContext.Accounts.InsertOnSubmit(account);
                }

                account.Name = tbName.Text.Trim();
                account.Url = tbFtp.Text.Trim();
                account.Username = tbUser.Text;
                account.Password = CryptorEngine.Encrypt(tbPass.Text, true, tbUser.Text);
                account.ImageFolder = tbPictureFolder.Text.Trim();
                account.ImagePrefix = tbPicturePrefix.Text.Trim();
                account.ThumbFolder = tbThumbFolder.Text.Trim();
                account.ThumbPrefix = tbThumbPrefix.Text.Trim();
                account.ImageWidth = rsc.BigWidth;
                account.ImageHeight = rsc.BigHeight;
                account.ThumbHeight = rsc.ThumbHeight;
                account.ThumbWidth = rsc.ThumbWidth;
                account.JpegQuality = rsc.JpegQuality;

                FormGM.DataContext.SubmitChanges();
                this.Close();
            }
        }

        private bool Valid() {
            string BigImageFolder = tbPictureFolder.Text.Trim();
            string BigImagePrefix = tbPicturePrefix.Text.Trim();
            string ThumbFolder = tbThumbFolder.Text.Trim();
            string ThumbPrefix = tbThumbPrefix.Text.Trim();
            if (BigImageFolder.Equals(ThumbFolder) && BigImagePrefix.Equals(ThumbPrefix)) {
                DialogResult result = MessageBox.Show("if ThumbFoldare and BigImageFolder is the same than prefixes must be diffrent.", "Error");
                if (result == DialogResult.OK || result == DialogResult.Cancel || true/* :) */) {
                    // :) Just need to wait until user will press ok (Modal Dialogue)
                }
                return false;
            }

            return true;
        }

        public static List<Account> GetAccounts() {
            IEnumerable<Account> accounts = FormGM.DataContext.Accounts.AsEnumerable<Account>();
            return accounts.ToList();
        }

        public static void SaveAccounts() {
            FormGM.DataContext.SubmitChanges();
        }

        private void FTPSettings_Load(object sender, System.EventArgs e) {

        }


    }
}
